package com.gok.einvoice.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gok.einvoice.common.exception.ServiceException;
import com.gok.einvoice.common.utils.bean.BeanUtils;
import com.gok.einvoice.web.dto.HotelInvoiceDTO;
import com.gok.einvoice.web.entity.InvoiceEntity;
import com.gok.einvoice.web.mapper.InvoiceMapper;
import com.gok.einvoice.web.service.InvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class InvoiceServiceImpl extends ServiceImpl<InvoiceMapper, InvoiceEntity> implements InvoiceService {

    /**
     * 新增酒店发票数据
     * @param dto
     * @return
     */
    @Override
    public String add(HotelInvoiceDTO dto) {
        if (ObjectUtils.isEmpty(dto)) {
            throw new ServiceException("发票数据不能为空");
        }

        String number = dto.getInvoiceNumber();
        LambdaQueryWrapper<InvoiceEntity> wrapper = new LambdaQueryWrapper<InvoiceEntity>()
                .eq(InvoiceEntity::getInvoiceNumber, number);
        if (!ObjectUtils.isEmpty(this.getOne(wrapper))) {
            throw new ServiceException("该记录已保存");
        }

        InvoiceEntity entity = new InvoiceEntity();
        BeanUtils.copyProperties(dto, entity);
        this.save(entity);
        return "保存成功";
    }
}
